#import "wooddoc.h"

@implementation WoodDoc (ValidateMenuCells)

- (BOOL)validateCommand:menuCell
{
    SEL action = [menuCell action];
	BOOL redraw = NO, enabled = [menuCell isEnabled];
	char buffer[100];

    if(action == @selector(revertToSaved:)){ 
		if(!enabled && [self needsSaving] && [self hasSavedDocument]){
			[menuCell setEnabled:YES];
			redraw = YES;
		} else if(enabled && (![self needsSaving] || ![self hasSavedDocument])){
			[menuCell setEnabled:NO];
			redraw = YES;
		};
	} else if(action == @selector(save:) || action == @selector(saveAs:) 
	        || action == @selector(print:) || action == @selector(saveTo:)){
		if(!enabled){
			[menuCell setEnabled:YES];
			redraw = YES;
		}
	} else if(action == @selector(changeZip:)){
		if(lastSelectedNode){
			if(!enabled){
				redraw = YES;
				[menuCell setEnabled:YES];
			}
			if([lastSelectedNode zipped] && strcmp([menuCell title], "Expand")){
				[menuCell setTitle:"Expand"];
				redraw = YES;
			} else if(![lastSelectedNode zipped] && strcmp([menuCell title], "Collapse")){
				[menuCell setTitle:"Collapse"];
				redraw = YES;
			}
		} else if(enabled){
			[menuCell setEnabled:NO];
			redraw = YES;
		} 
	} else if(action == @selector(copy:) || action == @selector(cut:) 
			|| action == @selector(delete:) || action == @selector(copyStyle:) 
			|| action == @selector(pasteStyle:) || action == @selector(promoteStyle:)){
		if(lastSelectedNode){
			if(!enabled){
				[menuCell setEnabled:YES];
				redraw = YES;
			}
		} else {
			if(enabled){
				[menuCell setEnabled:NO];
				redraw = YES;
			}
		}
	} else if(action == @selector(paste:) || action == @selector(add:) 
	       || action == @selector(addFromFilter:)){
		if(lastSelectedNode || !tree){
			if(!enabled){
				[menuCell setEnabled:YES];
				redraw = YES;
			}
		} else {
			if(enabled){
				[menuCell setEnabled:NO];
				redraw = YES;
			}
		}
	} else if(action == @selector(changeDescription:)){
		if(lastSelectedNode && !textViewSaved){
			if(!enabled){
				redraw = YES;
				[menuCell setEnabled:YES];
			}
		} else if(enabled){
			[menuCell setEnabled:NO];
			redraw = YES;
		} 
	} else if(action == @selector(toggleMarker:)){
		if(!enabled){
			redraw = YES;
			[menuCell setEnabled:YES];
		}
		if(showMarker && !strcmp([menuCell title], "Show Marker")){
				[menuCell setTitle:"Hide Marker"];
				redraw = YES;
		} else if(!showMarker && !strcmp([menuCell title], "Hide Marker")){
				[menuCell setTitle:"Show Marker"];
				redraw = YES;
		} 
	}  else if(action == @selector(undo:)){
		if([undoManager lastUndoName]){
			if(!enabled){
				redraw = YES;
				[menuCell setEnabled:YES];
			}
			sprintf(buffer,"Undo %s",[undoManager lastUndoName]);
			if(strcmp([menuCell title], buffer)){
				redraw = YES;
				[menuCell setTitle:buffer];
			}
		} else if(enabled){
			[menuCell setEnabled:NO];
			redraw = YES;
		} 
	} else if(action == @selector(redo:)){
		if([undoManager lastRedoName]){
			if(!enabled){
				redraw = YES;
				[menuCell setEnabled:YES];
			}
			sprintf(buffer,"Redo %s",[undoManager lastRedoName]);
			if(strcmp([menuCell title], buffer)){
				redraw = YES;
				[menuCell setTitle:buffer];
			}
		} else if(enabled){
			[menuCell setEnabled:NO];
			redraw = YES;
		} 
	}
    return redraw;
}

@end
